package com.qinchen.boot;

import ch.qos.logback.core.db.DBHelper;
import com.qinchen.boot.bean.Pet;
import com.qinchen.boot.bean.User;
import com.qinchen.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author qinchen
 * @date 2021/1/20 10:58 下午
 * 向未来的自己致敬
 * 描述：主程序类,这是一个SpringBoot应用
 * @SpringBootApplication相当于下面三个注解的合成注解
 *  @SpringBootConfiguration 底层其实就是配置类bean
 *  @EnableAutoConfiguration 核心这个家伙
 *      @AutoConfigurationPackage
 *      @Import
 *  @ComponentScan 指定扫描方式
 */
// @SpringBootApplication(scanBasePackages = {"com.qinchen"})
@SpringBootApplication(scanBasePackages = {"com.qinchen"})
public class MainApplication {

    public static void main(String[] args) {

        // 返回的是IOC容器
        ConfigurableApplicationContext applicationContext = SpringApplication.run(MainApplication.class, args);

        // 查看容器中所有的bean Name
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }

        // 从容器中获取组件，组件默认是单例的
        User user = applicationContext.getBean("user01", User.class);
        System.out.println(user);

        Pet tomcat = applicationContext.getBean("tomcat", Pet.class);
        System.out.println(tomcat);

        // 注意，配置类的实例默认为代理对象
        MyConfig myConfig = applicationContext.getBean(MyConfig.class);
        System.out.println(myConfig);

        String[] userBeans = applicationContext.getBeanNamesForType(User.class);
        for (String userBean : userBeans) {
            System.out.println("===> " + userBean);
        }

        String[] dbhelpBeans = applicationContext.getBeanNamesForType(DBHelper.class);
        for (String dbhelpBean : dbhelpBeans) {
            System.out.println("===> " + dbhelpBean);
        }

    }
}
